
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第二节 用vgg16实现图片识别 · Tensorflow学习笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="scottdu">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../chapter7/" />
    
    
    <link rel="prev" href="../chapter5/section6.1.html" />
    

    
    <link rel="stylesheet" href="../gitbook/gitbook-plugin-chart/c3/c3.min.css">
    <script src="../gitbook/gitbook-plugin-chart/c3/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-chart/c3/c3.min.js"></script>
    

    <script src="../gitbook/gitbook-plugin-graph/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-graph/function-plot.js"></script>    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter1/">
            
                <a href="../chapter1/">
            
                    
                    第一章 Tensorflow框架
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/section1.1.html">
            
                <a href="../chapter1/section1.1.html">
            
                    
                    第一节 张量、计算图、会话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/section1.2.html">
            
                <a href="../chapter1/section1.2.html">
            
                    
                    第二节 前向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/section1.3.html">
            
                <a href="../chapter1/section1.3.html">
            
                    
                    第三节 反向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/section1.4.html">
            
                <a href="../chapter1/section1.4.html">
            
                    
                    第四节 搭建神经网络的步骤
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter2/">
            
                <a href="../chapter2/">
            
                    
                    第二章 神经网络优化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter2/section2.1.html">
            
                <a href="../chapter2/section2.1.html">
            
                    
                    第一节 损失函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter2/section2.2.html">
            
                <a href="../chapter2/section2.2.html">
            
                    
                    第二节 学习率
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter2/section2.3.html">
            
                <a href="../chapter2/section2.3.html">
            
                    
                    第三节 滑动平均
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter2/section2.4.html">
            
                <a href="../chapter2/section2.4.html">
            
                    
                    第四节 正则化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter2/section2.5.html">
            
                <a href="../chapter2/section2.5.html">
            
                    
                    第五节 神经网络的搭建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter3/">
            
                <a href="../chapter3/">
            
                    
                    第三章 全连接网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/section3.1.html">
            
                <a href="../chapter3/section3.1.html">
            
                    
                    第一节 MINIST数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/section3.2.html">
            
                <a href="../chapter3/section3.2.html">
            
                    
                    第二节 模块化搭建神经网络方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/section3.3.html">
            
                <a href="../chapter3/section3.3.html">
            
                    
                    第三节 手写数字识别准确率输出
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter4/">
            
                <a href="../chapter4/">
            
                    
                    第四章 全连接网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter4/section4.1.html">
            
                <a href="../chapter4/section4.1.html">
            
                    
                    第一节 输入手写数字图片输出识别结果
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter4/section4.2.html">
            
                <a href="../chapter4/section4.2.html">
            
                    
                    第二节 制作数据集
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter5/">
            
                <a href="../chapter5/">
            
                    
                    第五章 卷积网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter5/section5.1.html">
            
                <a href="../chapter5/section5.1.html">
            
                    
                    第一节 卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter5/section5.2.html">
            
                <a href="../chapter5/section5.2.html">
            
                    
                    第二节 lenel5代码讲解
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="./">
            
                <a href="./">
            
                    
                    第六章 卷积网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/section6.1.html">
            
                <a href="../chapter5/section6.1.html">
            
                    
                    第一节 复现已有的卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.7.2" data-path="section6.2.html">
            
                <a href="section6.2.html">
            
                    
                    第二节 用vgg16实现图片识别
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter7/">
            
                <a href="../chapter7/">
            
                    
                    第七章 Tensorflow应用
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >第二节 用vgg16实现图片识别</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7B2C;&#x4E8C;&#x8282;-&#x7528;vgg16&#x5B9E;&#x73B0;&#x56FE;&#x7247;&#x8BC6;&#x522B;">&#x7B2C;&#x4E8C;&#x8282; &#x7528;vgg16&#x5B9E;&#x73B0;&#x56FE;&#x7247;&#x8BC6;&#x522B;</h1>
<h2 id="vgg-&#x5B9E;&#x73B0;&#x4EE3;&#x7801;&#x91CD;&#x70B9;&#x8BB2;&#x89E3;">VGG &#x5B9E;&#x73B0;&#x4EE3;&#x7801;&#x91CD;&#x70B9;&#x8BB2;&#x89E3;</h2>
<ul>
<li><code>x = tf.placeholder(tf.float32,shape = [BATCH_SIZE,IMAGE_PIXELS])</code></li>
</ul>
<p><code>tf.placeholder</code>: &#x7528;&#x4E8E;&#x4F20;&#x5165;&#x771F;&#x5B9E;&#x8BAD;&#x7EC3;&#x6837;&#x672C; / &#x6D4B;&#x8BD5; / &#x771F;&#x5B9E;&#x7279;&#x5F81; / &#x5F85;&#x5904;&#x7406;&#x7279;&#x5F81;&#x3002;&#x53EA;&#x662F;&#x5360;&#x4F4D;&#xFF0C;&#x4E0D;&#x5FC5;&#x7ED9;&#x51FA;&#x521D;&#x503C;&#x3002;
&#x7528;<code>sess.run</code>&#x7684;<code>feed_dict</code>&#x53C2;&#x6570;&#x4EE5;&#x5B57;&#x5178;&#x5F62;&#x5F0F;&#x5582;&#x5165; <code>x:, y_: sess.run(feed_dict = {x: ,y_: })</code>
BATCH_SIZE: &#x4E00;&#x6B21;&#x4F20;&#x5165;&#x7684;&#x4E2A;&#x6570;&#x3002;
IMAGE_PIXELS:&#x56FE;&#x50CF;&#x50CF;&#x7D20;&#x3002;</p>
<p>&#x4F8B;: <code>x = tf.placeholder(&quot;float&quot;,[1,224,224,3])</code>
<code>BATCH_SIZE</code>&#x4E3A;<code>1</code>&#xFF0C;&#x8868;&#x793A;&#x4E00;&#x6B21;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x3002;&#x56FE;&#x50CF;&#x50CF;&#x7D20;&#x4E3A;<code>[224,224,3]</code>&#x3002;</p>
<ul>
<li><code>w = tf.Variable(tf.random_normal())</code>: &#x4ECE;&#x6B63;&#x6001;&#x5206;&#x5E03;&#x4E2D;&#x7ED9;&#x51FA;&#x6743;&#x91CD;<code>w</code>&#x7684;&#x968F;&#x673A;&#x503C;&#x3002;
<code>b = tf.Variable(tf.zeros())</code>: &#x7EDF;&#x4E00;&#x5C06;&#x504F;&#x7F6E;<code>b</code>&#x521D;&#x59CB;&#x5316;&#x4E3A;<code>0</code>&#x3002;</li>
</ul>
<p>&#x6CE8;&#x610F;: &#x4EE5;&#x4E0A;&#x4E24;&#x884C;&#x51FD;&#x6570;<code>Variable</code>&#x4E2D;&#x7684;<code>V</code>&#x8981;&#x5927;&#x5199;&#xFF0C;<code>Variable</code>&#x5FC5;&#x987B;&#x7ED9;&#x521D;&#x503C;&#x3002;</p>
<ul>
<li><p><code>np.load np.save</code>: &#x5C06;&#x6570;&#x7EC4;&#x4EE5;&#x4E8C;&#x8FDB;&#x5236;&#x683C;&#x5F0F;&#x4FDD;&#x5B58;&#x5230;&#x78C1;&#x76D8;&#xFF0C;&#x6269;&#x5C55;&#x540D;&#x4E3A;<code>.npy</code>&#x3002;</p>
</li>
<li><p><code>.item()</code>: &#x904D;&#x5386;(&#x952E;&#x503C;&#x5BF9;)&#x3002;</p>
</li>
<li><p><code>tf.shape(a)</code>&#x548C;<code>a.get_shape()</code>&#x6BD4;&#x8F83;</p>
</li>
</ul>
<p>&#x76F8;&#x540C;&#x70B9;: &#x90FD;&#x53EF;&#x4EE5;&#x5F97;&#x5230;<code>tensor``a</code>&#x7684;&#x5C3A;&#x5BF8;
&#x4E0D;&#x540C;&#x70B9;: <code>tf.shape()</code>&#x4E2D;<code>a</code>&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x662F;<code>tensor</code>, <code>list</code>, <code>array</code>;&#x800C;<code>a.get_shape()</code>&#x4E2D; <code>a</code>&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x53EA;&#x80FD;&#x662F;<code>tensor</code>,&#x4E14;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x5143;&#x7EC4;(<code>tuple</code>)&#x3002;</p>
<p>&#x4F8B;:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
x=tf.constant([[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>],[<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>]]
y=[[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>],[<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>]]
z=np.arange(<span class="hljs-number">24</span>).reshape([<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]))
sess=tf.Session()
<span class="hljs-comment"># tf.shape()</span>
x_shape=tf.shape(x) <span class="hljs-comment"># x_shape &#x662F;&#x4E00;&#x4E2A; tensor</span>
y_shape=tf.shape(y) <span class="hljs-comment"># &lt;tf.Tensor &apos;Shape_2:0&apos; shape=(2,) dtype=int32&gt;</span>
z_shape=tf.shape(z) <span class="hljs-comment">#  &lt;tf.Tensor &apos;Shape_5:0&apos; shape=(3,) dtype=int32&gt;    </span>
<span class="hljs-keyword">print</span> sess.run(x_shape) <span class="hljs-comment"># &#x7ED3;&#x679C;:[2 3]</span>
<span class="hljs-keyword">print</span> sess.run(y_shape) <span class="hljs-comment"># &#x7ED3;&#x679C;:[2 3]</span>
<span class="hljs-keyword">print</span> sess.run(z_shape) <span class="hljs-comment"># &#x7ED3;&#x679C;:[2 3 4]</span>

<span class="hljs-comment">#a.get_shape()</span>
x_shape=x.get_shape() <span class="hljs-comment"># &#x8FD4;&#x56DE;&#x7684;&#x662F;TensorShape([Dimension(2), Dimension(3)]),&#x4E0D;&#x80FD;&#x4F7F;&#x7528; sess.run()&#xFF0C;&#x56E0;&#x4E3A;&#x8FD4;&#x56DE;&#x7684;&#x4E0D;&#x662F; tensor &#x6216; string,&#x800C; &#x662F;&#x5143;&#x7EC4;</span>
x_shape=x.get_shape().as_list() <span class="hljs-comment"># &#x53EF;&#x4EE5;&#x4F7F;&#x7528; as_list()&#x5F97;&#x5230;&#x5177;&#x4F53;&#x7684;&#x5C3A;&#x5BF8;&#xFF0C;x_shape=[2 3]</span>
y_shape=y.get_shape() <span class="hljs-comment"># AttributeError: &apos;list&apos; object has no attribute &apos;get_shape&apos;</span>
z_shape=z.get_shape() <span class="hljs-comment"># AttributeError: &apos;numpy.ndarray&apos; object has no attribute &apos;get_shape&apos;</span>
</code></pre>
<ul>
<li><p><code>tf.nn.bias_add(&#x4E58;&#x52A0;&#x548C;&#xFF0C;bias)</code>: &#x628A;<code>bias</code>&#x52A0;&#x5230;&#x4E58;&#x52A0;&#x548C;&#x4E0A;&#x3002;</p>
</li>
<li><p><code>tf.reshape(tensor, shape)</code>: &#x6539;&#x53D8; tensor &#x7684;&#x5F62;&#x72B6;</p>
</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># tensor &#x2018;t&#x2019; is [1, 2, 3, 4, 5, 6, 7, 8, 9]</span>
<span class="hljs-comment"># tensor &#x2018;t&#x2019; has shape [9]</span>
reshape(t, [<span class="hljs-number">3</span>, <span class="hljs-number">3</span>]) ==&gt;
[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>],
[<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>],
[<span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]]
<span class="hljs-comment">#&#x5982;&#x679C; shape &#x6709;&#x5143;&#x7D20;[-1],&#x8868;&#x793A;&#x5728;&#x8BE5;&#x7EF4;&#x5EA6;&#x6253;&#x5E73;&#x81F3;&#x4E00;&#x7EF4;</span>
<span class="hljs-comment"># -1 &#x5C06;&#x81EA;&#x52A8;&#x63A8;&#x5BFC;&#x5F97;&#x4E3A; 9:</span>
reshape(t, [<span class="hljs-number">2</span>, <span class="hljs-number">-1</span>]) ==&gt;
[[<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>],
[<span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>]]
</code></pre>
<ul>
<li><p><code>np.argsort(&#x5217;&#x8868;)</code>: &#x5BF9;&#x5217;&#x8868;&#x4ECE;&#x5C0F;&#x5230;&#x5927;&#x6392;&#x5E8F;&#x3002;</p>
</li>
<li><p>OS &#x6A21;&#x5757;</p>
</li>
</ul>
<p><code>os.getcwd()</code>: &#x8FD4;&#x56DE;&#x5F53;&#x524D;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;&#x3002;
<code>os.path.join(path1[,path2[,......]])</code>: 
&#x8FD4;&#x56DE;&#x503C;: &#x5C06;&#x591A;&#x4E2A;&#x8DEF;&#x5F84;&#x7EC4;&#x5408;&#x540E;&#x8FD4;&#x56DE;&#x3002;
&#x6CE8;&#x610F;: &#x7B2C;&#x4E00;&#x4E2A;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;&#x4E4B;&#x524D;&#x7684;&#x53C2;&#x6570;&#x5C06;&#x88AB;&#x5FFD;&#x7565;&#x3002;</p>
<p>&#x4F8B;:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span>  os
<span class="hljs-meta">&gt;&gt;&gt; </span>vgg16_path = os.path.join(os.getcwd(),<span class="hljs-string">&quot;vgg16.npy&quot;</span>)
<span class="hljs-comment">#&#x5F53;&#x524D;&#x76EE;&#x5F55;/vgg16.npy&#xFF0C;&#x7D22;&#x5F15;&#x5230; vgg16.npy &#x6587;&#x4EF6;</span>
</code></pre>
<ul>
<li><code>np.save</code>: &#x5199;&#x6570;&#x7EC4;&#x5230;&#x6587;&#x4EF6;(&#x672A;&#x538B;&#x7F29;&#x4E8C;&#x8FDB;&#x5236;&#x5F62;&#x5F0F;)&#xFF0C;&#x6587;&#x4EF6;&#x9ED8;&#x8BA4;&#x7684;&#x6269;&#x5C55;&#x540D;&#x662F;<code>.npy</code>&#x3002;<code>np.save(&quot;&#x540D;.npy&quot;&#xFF0C;&#x67D0;&#x6570;&#x7EC4;)</code>: &#x5C06;&#x67D0;&#x6570;&#x7EC4;&#x5199;&#x5165;&#x201C;&#x540D;.npy&#x201D;&#x6587;&#x4EF6;&#x3002;</li>
</ul>
<p><code>&#x67D0;&#x53D8;&#x91CF; = np.load(&quot;&#x540D;.npy&quot;&#xFF0C;encoding = &quot; &quot;).item()</code>: &#x5C06;&#x201C;&#x540D;.npy&#x201D;&#x6587;&#x4EF6;&#x8BFB; &#x51FA;&#x7ED9;&#x67D0;&#x53D8;&#x91CF;&#x3002;
<code>encoding = &quot; &quot;</code> &#x53EF;&#x4EE5;&#x4E0D;&#x5199;&#x2018;latin1&#x2019;&#x3001;&#x2018;ASCII&#x2019;&#x3001;&#x2018;bytes&#x2019;&#xFF0C; &#x9ED8;&#x8BA4;&#x4E3A;&#x2019;ASCII&#x2019;&#x3002;</p>
<p>&#x4F8B;:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
A = np.arange(<span class="hljs-number">15</span>).reshape(<span class="hljs-number">3</span>,<span class="hljs-number">5</span>) 
<span class="hljs-meta">&gt;&gt;&gt; </span>A
array([[ <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>],
       [ <span class="hljs-number">5</span>,  <span class="hljs-number">6</span>,  <span class="hljs-number">7</span>,  <span class="hljs-number">8</span>,  <span class="hljs-number">9</span>],
       [<span class="hljs-number">10</span>, <span class="hljs-number">11</span>, <span class="hljs-number">12</span>, <span class="hljs-number">13</span>, <span class="hljs-number">14</span>]])
<span class="hljs-meta">&gt;&gt;&gt; </span>np.save(<span class="hljs-string">&quot;A.npy&quot;</span>,A) <span class="hljs-comment">#&#x5982;&#x679C;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#x672B;&#x5C3E;&#x6CA1;&#x6709;&#x6269;&#x5C55;&#x540D;.npy&#xFF0C;&#x8BE5;&#x6269;&#x5C55;&#x540D;&#x4F1A;&#x88AB; &#x81EA;&#x52A8;&#x52A0;&#x4E0A;&#x3002;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>B=np.load(<span class="hljs-string">&quot;A.npy&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>B
array([[ <span class="hljs-number">0</span>,  <span class="hljs-number">1</span>,  <span class="hljs-number">2</span>,  <span class="hljs-number">3</span>,  <span class="hljs-number">4</span>],
       [ <span class="hljs-number">5</span>,  <span class="hljs-number">6</span>,  <span class="hljs-number">7</span>,  <span class="hljs-number">8</span>,  <span class="hljs-number">9</span>],
       [<span class="hljs-number">10</span>, <span class="hljs-number">11</span>, <span class="hljs-number">12</span>, <span class="hljs-number">13</span>, <span class="hljs-number">14</span>]])
</code></pre>
<ul>
<li><code>tf.split(dimension, num_split, input)</code>: </li>
</ul>
<p><code>dimension</code>: &#x8F93;&#x5165;&#x5F20;&#x91CF;&#x7684;&#x54EA;&#x4E00;&#x4E2A;&#x7EF4;&#x5EA6;&#xFF0C;&#x5982;&#x679C;&#x662F;0&#x5C31;&#x8868;&#x793A;&#x5BF9;&#x7B2C;0&#x7EF4;&#x5EA6;&#x8FDB;&#x884C;&#x5207;&#x5272;&#x3002; 
<code>num_split</code>: &#x5207;&#x5272;&#x7684;&#x6570;&#x91CF;&#xFF0C;&#x5982;&#x679C;&#x662F;2&#x5C31;&#x8868;&#x793A;&#x8F93;&#x5165;&#x5F20;&#x91CF;&#x88AB;&#x5207;&#x6210;2&#x4EFD;&#xFF0C;&#x6BCF;&#x4E00;&#x4EFD;&#x662F;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x3002;</p>
<p>&#x4F8B;:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf; 
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np;
A = [[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>],[<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>]]
x = tf.split(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, A)
<span class="hljs-keyword">with</span> tf.Session() <span class="hljs-keyword">as</span> sess:
  c = sess.run(x)
  <span class="hljs-keyword">for</span> ele <span class="hljs-keyword">in</span> c:
    print(ele)
</code></pre>
<p>&#x8F93;&#x51FA;:</p>
<pre><code class="lang-python">[[<span class="hljs-number">1</span>]
 [<span class="hljs-number">4</span>]] 
[[<span class="hljs-number">2</span>]
 [<span class="hljs-number">5</span>]] 
[[<span class="hljs-number">3</span>]
  [<span class="hljs-number">6</span>]]
</code></pre>
<ul>
<li><code>tf.concat(concat_dim, values)</code>:</li>
</ul>
<p>&#x6CBF;&#x7740;&#x67D0;&#x4E00;&#x7EF4;&#x5EA6;&#x8FDE;&#x7ED3;<code>tensor</code>:</p>
<pre><code class="lang-python">t1 = [[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]]
t2 = [[<span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>], [<span class="hljs-number">10</span>, <span class="hljs-number">11</span>, <span class="hljs-number">12</span>]]
tf.concat(<span class="hljs-number">0</span>, [t1, t2]) ==&gt; [[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>], [<span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>], [<span class="hljs-number">10</span>, <span class="hljs-number">11</span>, <span class="hljs-number">12</span>]]
tf.concat(<span class="hljs-number">1</span>, [t1, t2]) ==&gt; [[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>], [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">10</span>, <span class="hljs-number">11</span>, <span class="hljs-number">12</span>]]
</code></pre>
<p>&#x5982;&#x679C;&#x60F3;&#x6CBF;&#x7740;<code>tensor</code>&#x4E00;&#x65B0;&#x8F74;&#x8FDE;&#x7ED3;&#x6253;&#x5305;,&#x90A3;&#x4E48;&#x53EF;&#x4EE5;:</p>
<pre><code class="lang-python">tf.concat(axis, [tf.expand_dims(t, axis) <span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> tensors])
<span class="hljs-comment"># &#x7B49;&#x540C;&#x4E8E;</span>
tf.pack(tensors, axis=axis)
</code></pre>
<ul>
<li><p><code>fig = plt.figure(&quot;&#x56FE;&#x540D;&#x5B57;&quot;)</code>: &#x5B9E;&#x4F8B;&#x5316;&#x56FE;&#x5BF9;&#x8C61;&#x3002;</p>
</li>
<li><p><code>ax = fig.add_subplot(m n k)</code>: &#x5C06;&#x753B;&#x5E03;&#x5206;&#x5272;&#x6210;<code>m</code>&#x884C;<code>n</code>&#x5217;&#xFF0C;&#x56FE;&#x50CF;&#x753B;&#x5728;&#x4ECE;&#x5DE6;&#x5230; &#x53F3;&#x4ECE;&#x4E0A;&#x5230;&#x4E0B;&#x7684;&#x7B2C;<code>k</code>&#x5757;&#x3002;</p>
</li>
</ul>
<p>&#x4F8B;:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt 
<span class="hljs-keyword">from</span> numpy <span class="hljs-keyword">import</span> *
<span class="hljs-comment">#&#x7ED8;&#x56FE;</span>
fig = plt.figure()
ax = fig.add_subplot(<span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">9</span>) 
ax.plot(x,y)
plt.show()
</code></pre>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-08-16-15343573700338.jpg" alt=""></p>
<p><code>ax.bar(bar &#x7684;&#x4E2A;&#x6570;&#xFF0C;bar &#x7684;&#x503C;&#xFF0C;&#x6BCF;&#x4E2A; bar &#x7684;&#x540D;&#x5B57;&#xFF0C;bar &#x7684;&#x5BBD;&#xFF0C;bar &#x7684;&#x989C;&#x8272;)</code>: &#x7ED8;&#x5236;&#x76F4;&#x65B9;&#x56FE;&#x3002;&#x7ED9;&#x51FA;<code>bar</code>&#x7684;&#x4E2A;&#x6570;&#xFF0C;<code>bar</code>&#x7684;&#x503C;&#xFF0C;&#x6BCF;&#x4E2A;<code>bar</code>&#x7684;&#x540D;&#x5B57;&#xFF0C;<code>bar</code>&#x7684;&#x5BBD;&#xFF0C;<code>bar</code>&#x7684;&#x989C;&#x8272;&#x3002;</p>
<p><code>ax.set_ylabel(&quot;&quot;)</code>: &#x7ED9;&#x51FA;<code>y</code>&#x8F74;&#x7684;&#x540D;&#x5B57;&#x3002; 
<code>ax.set_title(&quot;&quot;)</code>: &#x7ED9;&#x51FA;&#x5B50;&#x56FE;&#x7684;&#x540D;&#x5B57;&#x3002;
<code>ax.text(x,y,string,fontsize=15,verticalalignment=&quot;top&quot;,horizontalalignment=&quot;right&quot;)</code>: 
x,y:&#x8868;&#x793A;&#x5750;&#x6807;&#x8F74;&#x4E0A;&#x7684;&#x503C;&#x3002;
string:&#x8868;&#x793A;&#x8BF4;&#x660E;&#x6587;&#x5B57;&#x3002;
fontsize:&#x8868;&#x793A;&#x5B57;&#x4F53;&#x5927;&#x5C0F;&#x3002;
verticalalignment:&#x5782;&#x76F4;&#x5BF9;&#x9F50;&#x65B9;&#x5F0F;&#xFF0C;&#x53C2;&#x6570;:[ &#x2018;center&#x2019; | &#x2018;top&#x2019; | &#x2018;bottom&#x2019; | &#x2018;baseline&#x2019; ]
horizontalalignment:&#x6C34;&#x5E73;&#x5BF9;&#x9F50;&#x65B9;&#x5F0F;&#xFF0C;&#x53C2;&#x6570;:[&#x2018;center&#x2019;|&#x2018;right&#x2019;|&#x2018;left&#x2019;] </p>
<p>xycoords &#x9009;&#x62E9;&#x6307;&#x5B9A;&#x7684;&#x5750;&#x6807;&#x8F74;&#x7CFB;&#x7EDF;:</p>
<p>(1)figure points
points from the lower left of the figure &#x70B9;&#x5728;&#x56FE;&#x5DE6;&#x4E0B;&#x65B9;</p>
<p>(2)figure pixels
pixels from the lower left of the figure &#x56FE;&#x5DE6;&#x4E0B;&#x89D2;&#x7684;&#x50CF;&#x7D20;</p>
<p>(3)figure fraction
fraction of figure from lower left &#x5DE6;&#x4E0B;&#x89D2;&#x6570;&#x5B57;&#x90E8;&#x5206;</p>
<p>(4)axes points
points from lower left corner of axes &#x4ECE;&#x5DE6;&#x4E0B;&#x89D2;&#x70B9;&#x7684;&#x5750;&#x6807;</p>
<p>(5)axes pixels
pixels from lower left corner of axes &#x4ECE;&#x5DE6;&#x4E0B;&#x89D2;&#x7684;&#x50CF;&#x7D20;&#x5750;&#x6807;</p>
<p>(6)axes fraction
fraction of axes from lower left &#x5DE6;&#x4E0B;&#x89D2;&#x90E8;&#x5206;</p>
<p>(7)data
use the coordinate system of the object being annotated(default) &#x4F7F;&#x7528;&#x7684;&#x5750;&#x6807;&#x7CFB;&#x7EDF;&#x88AB;&#x6CE8;&#x91CA;&#x7684;&#x5BF9;&#x8C61;(&#x9ED8;&#x8BA4;)</p>
<p>(8)polar(theta,r)</p>
<p>(9)if not native &#x2018;data&#x2019; coordinates t arrowprops #&#x7BAD;&#x5934;&#x53C2;&#x6570;,&#x53C2;&#x6570;&#x7C7B;
&#x578B;&#x4E3A;&#x5B57;&#x5178; dict</p>
<p>(10)width
the width of the arrow in points &#x70B9;&#x7BAD;&#x5934;&#x7684;&#x5BBD;&#x5EA6;</p>
<p>(11)headwidth
the width of the base of the arrow head in points &#x5728;&#x70B9;&#x7684;&#x7BAD;&#x5934;&#x5E95;&#x5EA7;&#x7684;&#x5BBD;&#x5EA6;</p>
<p>(12)headlength
the length of the arrow head in points &#x70B9;&#x7BAD;&#x5934;&#x7684;&#x957F;&#x5EA6;</p>
<p>(13)shrink
fraction of total length to &#x2018;shrink&#x2019; from both ends &#x603B;&#x957F;&#x5EA6;&#x4E3A;&#x5206;&#x6570;&#x201C;&#x7F29;&#x6C34;&#x201D;&#x4ECE;&#x4E24;&#x7AEF;</p>
<p>(14)facecolor &#x7BAD;&#x5934;&#x989C;&#x8272;
bbox &#x7ED9;&#x6807;&#x9898;&#x589E;&#x52A0;&#x5916;&#x6846; &#xFF0C;&#x5E38;&#x7528;&#x53C2;&#x6570;&#x5982;&#x4E0B;:
boxstyle &#x65B9;&#x6846;&#x5916;&#x5F62;
facecolor(&#x7B80;&#x5199; fc)&#x80CC;&#x666F;&#x989C;&#x8272;
edgecolor(&#x7B80;&#x5199; ec)&#x8FB9;&#x6846;&#x7EBF;&#x6761;&#x989C;&#x8272;
edgewidth &#x8FB9;&#x6846;&#x7EBF;&#x6761;&#x5927;&#x5C0F;</p>
<pre><code class="lang-python">bbox=dict(boxstyle=<span class="hljs-string">&apos;round,pad=0.5&apos;</span>,fc=<span class="hljs-string">&apos;yellow&apos;</span>,ec=<span class="hljs-string">&apos;k&apos;</span>,lw=<span class="hljs-number">1</span> ,alpha=<span class="hljs-number">0.5</span>) 
<span class="hljs-comment">#fc&#x4E3A; facecolor,ec &#x4E3A; edgecolor,lw &#x4E3A; lineweight</span>
</code></pre>
<ul>
<li><p><code>plt.show()</code>: &#x753B;&#x51FA;&#x6765;&#x3002;</p>
</li>
<li><p><code>axo = imshow(&#x56FE;)</code>: &#x753B;&#x5B50;&#x56FE;&#x3002;</p>
</li>
</ul>
<p>&#x56FE; = io.imread(&#x56FE;&#x8DEF;&#x5F84;&#x7D22;&#x5F15;&#x5230;&#x6587;&#x4EF6;)&#x3002;</p>
<ul>
<li><code>vgg</code>&#x7F51;&#x7EDC;&#x5177;&#x4F53;&#x7ED3;&#x6784;</li>
</ul>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-08-16-15343577840327.jpg" alt=""></p>
<ul>
<li><code>vgg16.py</code>&#x8FD8;&#x539F;&#x7F51;&#x7EDC;&#x548C;&#x53C2;&#x6570;</li>
</ul>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-08-16-15343578189253.jpg" alt=""></p>
<ul>
<li><code>app.py</code>&#x8BFB;&#x5165;&#x5F85;&#x5224;&#x56FE;&#xFF0C;&#x7ED9;&#x51FA;&#x53EF;&#x89C6;&#x5316;&#x7ED3;&#x679C;</li>
</ul>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-08-16-15343578559069.jpg" alt=""></p>
<h2 id="&#x8BFE;&#x7A0B;&#x4E2D;vgg&#x6E90;&#x7801;&#x7684;&#x5168;&#x6587;&#x6CE8;&#x91CA;">&#x8BFE;&#x7A0B;&#x4E2D;<code>VGG</code>&#x6E90;&#x7801;&#x7684;&#x5168;&#x6587;&#x6CE8;&#x91CA;</h2>
<p><code>vgg16.py</code></p>
<pre><code class="lang-python"><span class="hljs-comment">#!/usr/bin/python</span>
<span class="hljs-comment">#coding:utf-8</span>
<span class="hljs-keyword">import</span> inspect
<span class="hljs-keyword">import</span> os
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
VGG_MEAN = [<span class="hljs-number">103.939</span>, <span class="hljs-number">116.779</span>, <span class="hljs-number">123.68</span>] <span class="hljs-comment"># &#x6837;&#x672C; RGB &#x7684;&#x5E73;&#x5747;&#x503C;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vgg16</span><span class="hljs-params">()</span>:</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, vgg16_path=None)</span>:</span>
    <span class="hljs-keyword">if</span> vgg16_path <span class="hljs-keyword">is</span> <span class="hljs-keyword">None</span>:
      vgg16_path = os.path.join(os.getcwd(), <span class="hljs-string">&quot;vgg16.npy&quot;</span>) <span class="hljs-comment"># os.getcwd() &#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x8FD4;&#x56DE;&#x5F53;&#x524D;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;&#x3002;</span>
      print(vgg16_path)
      self.data_dict = np.load(vgg16_path, encoding=<span class="hljs-string">&apos;latin1&apos;</span>).item() <span class="hljs-comment"># &#x904D;&#x5386;&#x5176;&#x5185;&#x952E;&#x503C;&#x5BF9;&#xFF0C;&#x5BFC;&#x5165;&#x6A21;&#x578B;&#x53C2;&#x6570;</span>
    <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> self.data_dict: <span class="hljs-comment">#&#x904D;&#x5386; data_dict &#x4E2D;&#x7684;&#x6BCF;&#x4E2A;&#x952E;</span>
      print(x)

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">forward</span><span class="hljs-params">(self, images)</span>:</span>
    <span class="hljs-comment"># plt.figure(&quot;process pictures&quot;)</span>
    print(<span class="hljs-string">&quot;build model started&quot;</span>)
    start_time = time.time() <span class="hljs-comment"># &#x83B7;&#x53D6;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x7684;&#x5F00;&#x59CB;&#x65F6;&#x95F4;</span>
    rgb_scaled = images * <span class="hljs-number">255.0</span> <span class="hljs-comment"># &#x9010;&#x50CF;&#x7D20;&#x4E58;&#x4EE5; 255.0(&#x6839;&#x636E;&#x539F;&#x8BBA;&#x6587;&#x6240;&#x8FF0;&#x7684;&#x521D;&#x59CB;&#x5316;&#x6B65;&#x9AA4;)</span>
    <span class="hljs-comment"># &#x4ECE; GRB &#x8F6C;&#x6362;&#x8272;&#x5F69;&#x901A;&#x9053;&#x5230; BGR&#xFF0C;&#x4E5F;&#x53EF;&#x4F7F;&#x7528; cv &#x4E2D;&#x7684; GRBtoBGR</span>
    red, green, blue = tf.split(rgb_scaled,<span class="hljs-number">3</span>,<span class="hljs-number">3</span>)
    <span class="hljs-keyword">assert</span> red.get_shape().as_list()[<span class="hljs-number">1</span>:] == [<span class="hljs-number">224</span>, <span class="hljs-number">224</span>, <span class="hljs-number">1</span>]
    <span class="hljs-keyword">assert</span> green.get_shape().as_list()[<span class="hljs-number">1</span>:] == [<span class="hljs-number">224</span>, <span class="hljs-number">224</span>, <span class="hljs-number">1</span>]
    <span class="hljs-keyword">assert</span> blue.get_shape().as_list()[<span class="hljs-number">1</span>:] == [<span class="hljs-number">224</span>, <span class="hljs-number">224</span>, <span class="hljs-number">1</span>]
    <span class="hljs-comment"># &#x4EE5;&#x4E0A; assert &#x90FD;&#x662F;&#x52A0;&#x5165;&#x65AD;&#x8A00;&#xFF0C;&#x7528;&#x6765;&#x5224;&#x65AD;&#x6BCF;&#x4E2A;&#x64CD;&#x4F5C;&#x540E;&#x7684;&#x7EF4;&#x5EA6;&#x53D8;&#x5316;&#x662F;&#x5426;&#x548C;&#x9884;&#x671F;&#x4E00;&#x81F4;</span>
    bgr = tf.concat([blue - VGG_MEAN[<span class="hljs-number">0</span>], green - VGG_MEAN[<span class="hljs-number">1</span>], red - VGG_MEAN[<span class="hljs-number">2</span>]],<span class="hljs-number">3</span>)
    <span class="hljs-comment"># &#x9010;&#x6837;&#x672C;&#x51CF;&#x53BB;&#x6BCF;&#x4E2A;&#x901A;&#x9053;&#x7684;&#x50CF;&#x7D20;&#x5E73;&#x5747;&#x503C;&#xFF0C;&#x8FD9;&#x79CD;&#x64CD;&#x4F5C;&#x53EF;&#x4EE5;&#x79FB;&#x9664;&#x56FE;&#x50CF;&#x7684;&#x5E73;&#x5747;&#x4EAE;&#x5EA6;&#x503C;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x5E38;&#x7528;&#x5728;&#x7070;&#x5EA6;&#x56FE;&#x50CF;&#x4E0A;</span>
    <span class="hljs-keyword">assert</span> bgr.get_shape().as_list()[<span class="hljs-number">1</span>:] == [<span class="hljs-number">224</span>, <span class="hljs-number">224</span>, <span class="hljs-number">3</span>]
    <span class="hljs-comment"># &#x63A5;&#x4E0B;&#x6765;&#x6784;&#x5EFA; VGG &#x7684; 16 &#x5C42;&#x7F51;&#x7EDC;(&#x5305;&#x542B; 5 &#x6BB5;&#x5377;&#x79EF;&#xFF0C;3 &#x5C42;&#x5168;&#x8FDE;&#x63A5;)&#xFF0C;&#x5E76;&#x9010;&#x5C42;&#x6839;&#x636E;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x8BFB;&#x53D6;&#x7F51;&#x7EDC;&#x53C2;&#x6570;</span>
    <span class="hljs-comment"># &#x7B2C;&#x4E00;&#x6BB5;&#x5377;&#x79EF;&#xFF0C;&#x542B;&#x6709;&#x4E24;&#x4E2A;&#x5377;&#x79EF;&#x5C42;&#xFF0C;&#x540E;&#x9762;&#x63A5;&#x6700;&#x5927;&#x6C60;&#x5316;&#x5C42;&#xFF0C;&#x7528;&#x6765;&#x7F29;&#x5C0F;&#x56FE;&#x7247;&#x5C3A;&#x5BF8;</span>
    self.conv1_1 = self.conv_layer(bgr, <span class="hljs-string">&quot;conv1_1&quot;</span>)
    <span class="hljs-comment"># &#x4F20;&#x5165;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684; name&#xFF0C;&#x6765;&#x83B7;&#x53D6;&#x8BE5;&#x5C42;&#x7684;&#x5377;&#x79EF;&#x6838;&#x548C;&#x504F;&#x7F6E;&#xFF0C;&#x5E76;&#x505A;&#x5377;&#x79EF;&#x8FD0;&#x7B97;&#xFF0C;&#x6700;&#x540E;&#x8FD4;&#x56DE;&#x7ECF;&#x8FC7;&#x7ECF;&#x8FC7;&#x6FC0;&#x6D3B;&#x51FD;&#x6570;&#x540E;&#x7684;&#x503C;</span>
    self.conv1_2 = self.conv_layer(self.conv1_1, <span class="hljs-string">&quot;conv1_2&quot;</span>)
    <span class="hljs-comment"># &#x6839;&#x636E;&#x4F20;&#x5165;&#x7684; pooling &#x540D;&#x5B57;&#x5BF9;&#x8BE5;&#x5C42;&#x505A;&#x76F8;&#x5E94;&#x7684;&#x6C60;&#x5316;&#x64CD;&#x4F5C;</span>
    self.pool1 = self.max_pool_2x2(self.conv1_2, <span class="hljs-string">&quot;pool1&quot;</span>)

    <span class="hljs-comment"># &#x4E0B;&#x9762;&#x7684;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x4E0E;&#x7B2C;&#x4E00;&#x6BB5;&#x540C;&#x7406;</span>
    <span class="hljs-comment"># &#x7B2C;&#x4E8C;&#x6BB5;&#x5377;&#x79EF;&#xFF0C;&#x540C;&#x6837;&#x5305;&#x542B;&#x4E24;&#x4E2A;&#x5377;&#x79EF;&#x5C42;&#xFF0C;&#x4E00;&#x4E2A;&#x6700;&#x5927;&#x6C60;&#x5316;&#x5C42;</span>
    self.conv2_1 = self.conv_layer(self.pool1, <span class="hljs-string">&quot;conv2_1&quot;</span>) 
    self.conv2_2 = self.conv_layer(self.conv2_1, <span class="hljs-string">&quot;conv2_2&quot;</span>) 
    self.pool2 = self.max_pool_2x2(self.conv2_2, <span class="hljs-string">&quot;pool2&quot;</span>)

    <span class="hljs-comment"># &#x7B2C;&#x4E09;&#x6BB5;&#x5377;&#x79EF;&#xFF0C;&#x5305;&#x542B;&#x4E09;&#x4E2A;&#x5377;&#x79EF;&#x5C42;&#xFF0C;&#x4E00;&#x4E2A;&#x6700;&#x5927;&#x6C60;&#x5316;&#x5C42;</span>
    elf.conv3_1 = self.conv_layer(self.pool2, <span class="hljs-string">&quot;conv3_1&quot;</span>) 
    self.conv3_2 = self.conv_layer(self.conv3_1, <span class="hljs-string">&quot;conv3_2&quot;</span>) 
    self.conv3_3 = self.conv_layer(self.conv3_2, <span class="hljs-string">&quot;conv3_3&quot;</span>) 
    self.pool3 = self.max_pool_2x2(self.conv3_3, <span class="hljs-string">&quot;pool3&quot;</span>)

    <span class="hljs-comment"># &#x7B2C;&#x56DB;&#x6BB5;&#x5377;&#x79EF;&#xFF0C;&#x5305;&#x542B;&#x4E09;&#x4E2A;&#x5377;&#x79EF;&#x5C42;&#xFF0C;&#x4E00;&#x4E2A;&#x6700;&#x5927;&#x6C60;&#x5316;&#x5C42;</span>
    self.conv4_1 = self.conv_layer(self.pool3, <span class="hljs-string">&quot;conv4_1&quot;</span>) 
    self.conv4_2 = self.conv_layer(self.conv4_1, <span class="hljs-string">&quot;conv4_2&quot;</span>)
    self.conv4_3 = self.conv_layer(self.conv4_2, <span class="hljs-string">&quot;conv4_3&quot;</span>) 
    self.pool4 = self.max_pool_2x2(self.conv4_3, <span class="hljs-string">&quot;pool4&quot;</span>)

    <span class="hljs-comment"># &#x7B2C;&#x4E94;&#x6BB5;&#x5377;&#x79EF;&#xFF0C;&#x5305;&#x542B;&#x4E09;&#x4E2A;&#x5377;&#x79EF;&#x5C42;&#xFF0C;&#x4E00;&#x4E2A;&#x6700;&#x5927;&#x6C60;&#x5316;&#x5C42;</span>
    elf.conv5_1 = self.conv_layer(self.pool4, <span class="hljs-string">&quot;conv5_1&quot;</span>) 
    self.conv5_2 = self.conv_layer(self.conv5_1, <span class="hljs-string">&quot;conv5_2&quot;</span>) 
    self.conv5_3 = self.conv_layer(self.conv5_2, <span class="hljs-string">&quot;conv5_3&quot;</span>) 
    self.pool5 = self.max_pool_2x2(self.conv5_3, <span class="hljs-string">&quot;pool5&quot;</span>)

    <span class="hljs-comment"># &#x7B2C;&#x516D;&#x5C42;&#x5168;&#x8FDE;&#x63A5;</span>
    self.fc6 = self.fc_layer(self.pool5, <span class="hljs-string">&quot;fc6&quot;</span>) <span class="hljs-comment"># &#x6839;&#x636E;&#x547D;&#x540D;&#x7A7A;&#x95F4;name&#x505A;&#x52A0;&#x6743;&#x6C42;&#x548C;&#x8FD0;&#x7B97;</span>
    <span class="hljs-keyword">assert</span> self.fc6.get_shape().as_list()[<span class="hljs-number">1</span>:] == [<span class="hljs-number">4096</span>] <span class="hljs-comment"># 4096 &#x662F;&#x8BE5;&#x5C42;&#x8F93;&#x51FA;&#x540E;&#x7684;&#x957F;&#x5EA6; </span>
    self.relu6 = tf.nn.relu(self.fc6) <span class="hljs-comment"># &#x7ECF;&#x8FC7; relu &#x6FC0;&#x6D3B;&#x51FD;&#x6570;</span>

    <span class="hljs-comment"># &#x7B2C;&#x4E03;&#x5C42;&#x5168;&#x8FDE;&#x63A5;&#xFF0C;&#x548C;&#x4E0A;&#x4E00;&#x5C42;&#x540C;&#x7406;</span>
    self.fc7 = self.fc_layer(self.relu6, <span class="hljs-string">&quot;fc7&quot;</span>) 
    self.relu7 = tf.nn.relu(self.fc7)

    <span class="hljs-comment"># &#x7B2C;&#x516B;&#x5C42;&#x5168;&#x8FDE;&#x63A5;</span>
    self.fc8 = self.fc_layer(self.relu7, <span class="hljs-string">&quot;fc8&quot;</span>)
    <span class="hljs-comment"># &#x7ECF;&#x8FC7;&#x6700;&#x540E;&#x4E00;&#x5C42;&#x7684;&#x5168;&#x8FDE;&#x63A5;&#x540E;&#xFF0C;&#x518D;&#x505A; softmax &#x5206;&#x7C7B;&#xFF0C;&#x5F97;&#x5230;&#x5C5E;&#x4E8E;&#x5404;&#x7C7B;&#x522B;&#x7684;&#x6982;&#x7387;</span>
    self.prob = tf.nn.softmax(self.fc8, name=<span class="hljs-string">&quot;prob&quot;</span>)
    end_time = time.time() <span class="hljs-comment"># &#x5F97;&#x5230;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x7684;&#x7ED3;&#x675F;&#x65F6;&#x95F4;</span>
    print((<span class="hljs-string">&quot;time consuming: %f&quot;</span> % (end_time-start_time)))
    self.data_dict = <span class="hljs-keyword">None</span> <span class="hljs-comment"># &#x6E05;&#x7A7A;&#x672C;&#x6B21;&#x8BFB;&#x53D6;&#x5230;&#x7684;&#x6A21;&#x578B;&#x53C2;&#x6570;&#x5B57;&#x5178;</span>


  <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x5377;&#x79EF;&#x8FD0;&#x7B97;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">conv_layer</span><span class="hljs-params">(self, x, name)</span>:</span>
    <span class="hljs-keyword">with</span> tf.variable_scope(name): <span class="hljs-comment"># &#x6839;&#x636E;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x627E;&#x5230;&#x5BF9;&#x5E94;&#x5377;&#x79EF;&#x5C42;&#x7684;&#x7F51;&#x7EDC;&#x53C2;&#x6570;</span>
      conv = tf.nn.conv2d(x, w, [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>], padding=<span class="hljs-string">&apos;SAME&apos;</span>) <span class="hljs-comment"># &#x5377;&#x79EF;&#x8BA1;&#x7B97;</span>
      conv_biases = self.get_bias(name) <span class="hljs-comment"># &#x8BFB;&#x5230;&#x504F;&#x7F6E;&#x9879;</span>
      result = tf.nn.relu(tf.nn.bias_add(conv, conv_biases)) <span class="hljs-comment"># &#x52A0;&#x4E0A;&#x504F;&#x7F6E;&#xFF0C;&#x5E76;&#x505A;&#x6FC0;&#x6D3B;&#x8BA1;&#x7B97;</span>
      <span class="hljs-keyword">return</span> result

  <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x83B7;&#x53D6;&#x5377;&#x79EF;&#x6838;&#x7684;&#x51FD;&#x6570;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_conv_filter</span><span class="hljs-params">(self, name)</span>:</span>
    <span class="hljs-comment"># &#x6839;&#x636E;&#x547D;&#x540D;&#x7A7A;&#x95F4; name &#x4ECE;&#x53C2;&#x6570;&#x5B57;&#x5178;&#x4E2D;&#x53D6;&#x5230;&#x5BF9;&#x5E94;&#x7684;&#x5377;&#x79EF;&#x6838;</span>
    <span class="hljs-keyword">return</span> tf.constant(self.data_dict[name][<span class="hljs-number">0</span>], name=<span class="hljs-string">&quot;filter&quot;</span>)

  <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x83B7;&#x53D6;&#x504F;&#x7F6E;&#x9879;&#x7684;&#x51FD;&#x6570;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_bias</span><span class="hljs-params">(self, name)</span>:</span>
    <span class="hljs-comment"># &#x6839;&#x636E;&#x547D;&#x540D;&#x7A7A;&#x95F4; name &#x4ECE;&#x53C2;&#x6570;&#x5B57;&#x5178;&#x4E2D;&#x53D6;&#x5230;&#x5BF9;&#x5E94;&#x7684;&#x5377;&#x79EF;&#x6838;</span>
    <span class="hljs-keyword">return</span> tf.constant(self.data_dict[name][<span class="hljs-number">1</span>], name=<span class="hljs-string">&quot;biases&quot;</span>)

  <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x6700;&#x5927;&#x6C60;&#x5316;&#x64CD;&#x4F5C;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">max_pool_2x2</span><span class="hljs-params">(self, x, name)</span>:</span>
    <span class="hljs-keyword">return</span> tf.nn.max_pool(x, ksize=[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>], strides=[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>], padding=<span class="hljs-string">&apos;SAME&apos;</span>, name=name)

  <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x5168;&#x8FDE;&#x63A5;&#x5C42;&#x7684;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x8BA1;&#x7B97;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fc_layer</span><span class="hljs-params">(self, x, name)</span>:</span>
    <span class="hljs-keyword">with</span> tf.variable_scope(name): <span class="hljs-comment"># &#x6839;&#x636E;&#x547D;&#x540D;&#x7A7A;&#x95F4; name &#x505A;&#x5168;&#x8FDE;&#x63A5;&#x5C42;&#x7684;&#x8BA1;&#x7B97;  </span>
      shape = x.get_shape().as_list() <span class="hljs-comment"># &#x83B7;&#x53D6;&#x8BE5;&#x5C42;&#x7684;&#x7EF4;&#x5EA6;&#x4FE1;&#x606F;&#x5217;&#x8868;</span>
      print(<span class="hljs-string">&quot;fc_layer shape&quot;</span>, shape)
      dim = <span class="hljs-number">1</span>
      <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> shape[<span class="hljs-number">1</span>:]:
        dim *= i <span class="hljs-comment"># &#x5C06;&#x6BCF;&#x5C42;&#x7684;&#x7EF4;&#x5EA6;&#x76F8;&#x4E58;</span>
      <span class="hljs-comment"># &#x6539;&#x53D8;&#x7279;&#x5F81;&#x56FE;&#x7684;&#x5F62;&#x72B6;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5C06;&#x5F97;&#x5230;&#x7684;&#x591A;&#x7EF4;&#x7279;&#x5F81;&#x505A;&#x62C9;&#x4F38;&#x64CD;&#x4F5C;&#xFF0C;&#x53EA;&#x5728;&#x8FDB;&#x5165;&#x7B2C;&#x516D;&#x5C42;&#x5168;&#x8FDE;&#x63A5;&#x5C42;&#x505A;&#x8BE5;&#x64CD;&#x4F5C;</span>
      x = tf.reshape(x, [<span class="hljs-number">-1</span>, dim])
      w = self.get_fc_weight(name)<span class="hljs-comment"># &#x8BFB;&#x5230;&#x6743;&#x91CD;&#x503C;</span>
      b = self.get_bias(name) <span class="hljs-comment"># &#x8BFB;&#x5230;&#x504F;&#x7F6E;&#x9879;&#x503C;</span>
    result = tf.nn.bias_add(tf.matmul(x, w), b) <span class="hljs-comment"># &#x5BF9;&#x8BE5;&#x5C42;&#x8F93;&#x5165;&#x505A;&#x52A0;&#x6743;&#x6C42;&#x548C;&#xFF0C;&#x518D;&#x52A0;&#x4E0A;&#x504F;&#x7F6E;</span>
    <span class="hljs-keyword">return</span> result

  <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x83B7;&#x53D6;&#x6743;&#x91CD;&#x7684;&#x51FD;&#x6570;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_fc_weight</span><span class="hljs-params">(self, name)</span>:</span> <span class="hljs-comment"># &#x6839;&#x636E;&#x547D;&#x540D;&#x7A7A;&#x95F4; name &#x4ECE;&#x53C2;&#x6570;&#x5B57;&#x5178;&#x4E2D;&#x53D6;&#x5230;&#x5BF9;&#x5E94;&#x7684;&#x6743;&#x91CD;</span>
    <span class="hljs-keyword">return</span> tf.constant(self.data_dict[name][<span class="hljs-number">0</span>], name=<span class="hljs-string">&quot;weights&quot;</span>)
</code></pre>
<p><code>utils.py</code></p>
<pre><code class="lang-python"><span class="hljs-comment">#!/usr/bin/python </span>
<span class="hljs-comment">#coding:utf-8</span>
<span class="hljs-keyword">from</span> skimage 
<span class="hljs-keyword">import</span> io, transform 
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt 
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">from</span> pylab <span class="hljs-keyword">import</span> mpl
mpl.rcParams[<span class="hljs-string">&apos;font.sans-serif&apos;</span>]=[<span class="hljs-string">&apos;SimHei&apos;</span>] <span class="hljs-comment"># &#x6B63;&#x5E38;&#x663E;&#x793A;&#x4E2D;&#x6587;&#x6807;&#x7B7E; mpl.rcParams[&apos;axes.unicode_minus&apos;]=False # &#x6B63;&#x5E38;&#x663E;&#x793A;&#x6B63;&#x8D1F;&#x53F7;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">load_image</span><span class="hljs-params">(path)</span>:</span>
  fig = plt.figure(<span class="hljs-string">&quot;Centre and Resize&quot;</span>)
  img = io.imread(path) <span class="hljs-comment"># &#x6839;&#x636E;&#x4F20;&#x5165;&#x7684;&#x8DEF;&#x5F84;&#x8BFB;&#x5165;&#x56FE;&#x7247;</span>
  img = img / <span class="hljs-number">255.0</span> <span class="hljs-comment"># &#x5C06;&#x50CF;&#x7D20;&#x5F52;&#x4E00;&#x5316;&#x5230;[0,1]</span>
  <span class="hljs-comment"># &#x5C06;&#x8BE5;&#x753B;&#x5E03;&#x5206;&#x4E3A;&#x4E00;&#x884C;&#x4E09;&#x5217;</span>
  ax0 = fig.add_subplot(<span class="hljs-number">131</span>) <span class="hljs-comment"># &#x628A;&#x4E0B;&#x9762;&#x7684;&#x56FE;&#x50CF;&#x653E;&#x5728;&#x8BE5;&#x753B;&#x5E03;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x4F4D;&#x7F6E; </span>
  ax0.set_xlabel(<span class="hljs-string">u&apos;Original Picture&apos;</span>) <span class="hljs-comment"># &#x6DFB;&#x52A0;&#x5B50;&#x6807;&#x7B7E;</span>
  ax0.imshow(img) <span class="hljs-comment"># &#x6DFB;&#x52A0;&#x5C55;&#x793A;&#x8BE5;&#x56FE;&#x50CF;</span>
  short_edge = min(img.shape[:<span class="hljs-number">2</span>]) <span class="hljs-comment"># &#x627E;&#x5230;&#x8BE5;&#x56FE;&#x50CF;&#x7684;&#x6700;&#x77ED;&#x8FB9;</span>
  y = (img.shape[<span class="hljs-number">0</span>] - short_edge) / <span class="hljs-number">2</span>
  x = (img.shape[<span class="hljs-number">1</span>] - short_edge) / <span class="hljs-number">2</span> <span class="hljs-comment"># &#x628A;&#x56FE;&#x50CF;&#x7684; w &#x548C; h &#x5206;&#x522B;&#x51CF;&#x53BB;&#x6700;&#x77ED;&#x8FB9;&#xFF0C;&#x5E76;&#x6C42;&#x5E73;&#x5747; crop_img = img[y:y+short_edge, x:x+short_edge] # &#x53D6;&#x51FA;&#x5207;&#x5206;&#x51FA;&#x7684;&#x4E2D;&#x5FC3;&#x56FE;&#x50CF;</span>
  print(crop_img.shape)
  ax1 = fig.add_subplot(<span class="hljs-number">132</span>) <span class="hljs-comment"># &#x628A;&#x4E0B;&#x9762;&#x7684;&#x56FE;&#x50CF;&#x653E;&#x5728;&#x8BE5;&#x753B;&#x5E03;&#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x4F4D;&#x7F6E; </span>
  ax1.set_xlabel(<span class="hljs-string">u&quot;Centre Picture&quot;</span>) <span class="hljs-comment"># &#x6DFB;&#x52A0;&#x5B50;&#x6807;&#x7B7E; ax1.imshow(crop_img)</span>
  re_img = transform.resize(crop_img, (<span class="hljs-number">224</span>, <span class="hljs-number">224</span>)) <span class="hljs-comment"># resize &#x6210;&#x56FA;&#x5B9A;&#x7684; imag_szie</span>
  ax2 = fig.add_subplot(<span class="hljs-number">133</span>) <span class="hljs-comment"># &#x628A;&#x4E0B;&#x9762;&#x7684;&#x56FE;&#x50CF;&#x653E;&#x5728;&#x8BE5;&#x753B;&#x5E03;&#x7684;&#x7B2C;&#x4E09;&#x4E2A;&#x4F4D;&#x7F6E; </span>
  ax2.set_xlabel(<span class="hljs-string">u&quot;Resize Picture&quot;</span>) <span class="hljs-comment"># &#x6DFB;&#x52A0;&#x5B50;&#x6807;&#x7B7E;</span>
  ax2.imshow(re_img)
  img_ready = re_img.reshape((<span class="hljs-number">1</span>, <span class="hljs-number">224</span>, <span class="hljs-number">224</span>, <span class="hljs-number">3</span>))
  <span class="hljs-keyword">return</span> img_ready

<span class="hljs-comment"># &#x5B9A;&#x4E49;&#x767E;&#x5206;&#x6BD4;&#x8F6C;&#x6362;&#x51FD;&#x6570;</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">percent</span><span class="hljs-params">(value)</span>:</span>
  <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;%.2f%%&apos;</span> % (value * <span class="hljs-number">100</span>)
</code></pre>
<p><code>app.py</code></p>
<pre><code class="lang-python">
<span class="hljs-comment">#coding:utf-8</span>
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-comment"># Linux &#x670D;&#x52A1;&#x5668;&#x6CA1;&#x6709; GUI &#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x4F7F;&#x7528; matplotlib &#x7ED8;&#x56FE;&#xFF0C;&#x5FC5;&#x987B;&#x7F6E;&#x4E8E; pyplot &#x4E4B;&#x524D;</span>
<span class="hljs-comment">#import matplotlib</span>
<span class="hljs-comment">#matplotlib.use(&apos;Agg&apos;)</span>
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-comment"># &#x4E0B;&#x9762;&#x4E09;&#x4E2A;&#x662F;&#x5F15;&#x7528;&#x81EA;&#x5B9A;&#x4E49;&#x6A21;&#x5757; </span>
<span class="hljs-keyword">import</span> vgg16
<span class="hljs-keyword">import</span> utils
<span class="hljs-keyword">from</span> Nclasses <span class="hljs-keyword">import</span> labels
img_path = raw_input(<span class="hljs-string">&apos;Input the path and image name:&apos;</span>)
img_ready = utils.load_image(img_path) <span class="hljs-comment"># &#x8C03;&#x7528; load_image()&#x51FD;&#x6570;&#xFF0C;&#x5BF9;&#x5F85;&#x6D4B;&#x8BD5;&#x7684;&#x56FE;&#x50CF;&#x505A;&#x4E00;&#x4E9B;&#x9884;&#x5904;&#x7406;&#x64CD;&#x4F5C;</span>
<span class="hljs-comment">#&#x5B9A;&#x4E49;&#x4E00;&#x4E2A; figure &#x753B;&#x56FE;&#x7A97;&#x53E3;&#xFF0C;&#x5E76;&#x6307;&#x5B9A;&#x7A97;&#x53E3;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x7A97;&#x53E3;&#x4FEE;&#x7684;&#x5927;&#x5C0F; </span>
fig=plt.figure(<span class="hljs-string">u&quot;Top-5 &#x9884;&#x6D4B;&#x7ED3;&#x679C;&quot;</span>)
<span class="hljs-keyword">with</span> tf.Session() <span class="hljs-keyword">as</span> sess:
  <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7EF4;&#x5EA6;&#x4E3A;[1,224,224,3],&#x7C7B;&#x578B;&#x4E3A; float32 &#x7684; tensor &#x5360;&#x4F4D;&#x7B26;</span>
  x = tf.placeholder(tf.float32, [<span class="hljs-number">1</span>, <span class="hljs-number">224</span>, <span class="hljs-number">224</span>, <span class="hljs-number">3</span>])
  vgg = vgg16.Vgg16() <span class="hljs-comment"># &#x7C7B; Vgg16 &#x5B9E;&#x4F8B;&#x5316;&#x51FA; vgg</span>
  <span class="hljs-comment"># &#x8C03;&#x7528;&#x7C7B;&#x7684;&#x6210;&#x5458;&#x65B9;&#x6CD5; forward()&#xFF0C;&#x5E76;&#x4F20;&#x5165;&#x5F85;&#x6D4B;&#x8BD5;&#x56FE;&#x50CF;&#xFF0C;&#x8FD9;&#x4E5F;&#x5C31;&#x662F;&#x7F51;&#x7EDC;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x7684;&#x8FC7;&#x7A0B; </span>
  vgg.forward(x)
  <span class="hljs-comment"># &#x5C06;&#x4E00;&#x4E2A; batch &#x7684;&#x6570;&#x636E;&#x5582;&#x5165;&#x7F51;&#x7EDC;&#xFF0C;&#x5F97;&#x5230;&#x7F51;&#x7EDC;&#x7684;&#x9884;&#x6D4B;&#x8F93;&#x51FA;</span>
  probablity = sess.run(vgg.prob, feed_dict={x:img_ready})
  <span class="hljs-comment"># np.argsort &#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x9884;&#x6D4B;&#x503C;(probability &#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;[[&#x5404;&#x9884;&#x6D4B;&#x7C7B;&#x522B;&#x7684;&#x6982;&#x7387;&#x503C;]])&#x7531;&#x5C0F;&#x5230;&#x5927;&#x7684;&#x7D22;&#x5F15;&#x503C;&#xFF0C; </span>
  <span class="hljs-comment"># &#x5E76;&#x53D6;&#x51FA;&#x9884;&#x6D4B;&#x6982;&#x7387;&#x6700;&#x5927;&#x7684;&#x4E94;&#x4E2A;&#x7D22;&#x5F15;&#x503C;</span>
  top5 = np.argsort(probability[<span class="hljs-number">0</span>])[<span class="hljs-number">-1</span>:<span class="hljs-number">-6</span>:<span class="hljs-number">-1</span>]
  print(<span class="hljs-string">&quot;top5:&quot;</span>,top5)

<span class="hljs-comment"># &#x5B9A;&#x4E49;&#x4E24;&#x4E2A; list---&#x5BF9;&#x5E94;&#x7684;&#x6982;&#x7387;&#x503C;&#x548C;&#x5B9E;&#x9645;&#x6807;&#x7B7E;(zebra)</span>
values = []
bar_label = []
<span class="hljs-keyword">for</span> n, i <span class="hljs-keyword">in</span> enumerate(top5): <span class="hljs-comment"># &#x679A;&#x4E3E;&#x4E0A;&#x9762;&#x53D6;&#x51FA;&#x7684;&#x4E94;&#x4E2A;&#x7D22;&#x5F15;&#x503C;</span>
  print(<span class="hljs-string">&quot;n:&quot;</span>,n)
  print(<span class="hljs-string">&quot;i:&quot;</span>,i)
  values.append(probability[<span class="hljs-number">0</span>][i]) <span class="hljs-comment"># &#x5C06;&#x7D22;&#x5F15;&#x503C;&#x5BF9;&#x5E94;&#x7684;&#x9884;&#x6D4B;&#x6982;&#x7387;&#x503C;&#x53D6;&#x51FA;&#x5E76;&#x653E;&#x5165; values  </span>
  bar_label.append(labels[i]) <span class="hljs-comment"># &#x6839;&#x636E;&#x7D22;&#x5F15;&#x503C;&#x53D6;&#x51FA;&#x5BF9;&#x5E94;&#x7684;&#x5B9E;&#x9645;&#x6807;&#x7B7E;&#x5E76;&#x653E;&#x5165; bar_label</span>
  print(i, <span class="hljs-string">&quot;:&quot;</span>, labels[i], <span class="hljs-string">&quot;----&quot;</span>, utils.percent(probability[<span class="hljs-number">0</span>][i])) <span class="hljs-comment"># &#x6253;&#x5370;&#x5C5E;&#x4E8E;&#x67D0;&#x4E2A;&#x7C7B;&#x522B;&#x7684;&#x6982;&#x7387;</span>
ax = fig.add_subplot(<span class="hljs-number">111</span>) <span class="hljs-comment"># &#x5C06;&#x753B;&#x5E03;&#x5212;&#x5206;&#x4E3A;&#x4E00;&#x884C;&#x4E00;&#x5217;&#xFF0C;&#x5E76;&#x628A;&#x4E0B;&#x56FE;&#x653E;&#x5165;&#x5176;&#x4E2D;</span>
<span class="hljs-comment"># bar()&#x51FD;&#x6570;&#x7ED8;&#x5236;&#x67F1;&#x72B6;&#x56FE;&#xFF0C;&#x53C2;&#x6570; range(len(values)&#x662F;&#x67F1;&#x5B50;&#x4E0B;&#x6807;&#xFF0C;values &#x8868;&#x793A;&#x67F1;&#x9AD8;&#x7684;&#x5217;&#x8868;(&#x4E5F;&#x5C31;&#x662F;&#x4E94;&#x4E2A;&#x9884;&#x6D4B;&#x6982;&#x7387;&#x503C;&#xFF0C;</span>
<span class="hljs-comment"># tick_label &#x662F;&#x6BCF;&#x4E2A;&#x67F1;&#x5B50;&#x4E0A;&#x663E;&#x793A;&#x7684;&#x6807;&#x7B7E;(&#x5B9E;&#x9645;&#x5BF9;&#x5E94;&#x7684;&#x6807;&#x7B7E;)&#xFF0C;width &#x662F;&#x67F1;&#x5B50;&#x7684;&#x5BBD;&#x5EA6;&#xFF0C;fc &#x662F;&#x67F1;&#x5B50;&#x7684;&#x989C;&#x8272;)</span>
ax.bar(range(len(values)), values, tick_label=bar_label, width=<span class="hljs-number">0.5</span>, fc=<span class="hljs-string">&apos;g&apos;</span>)
ax.set_ylabel(<span class="hljs-string">u&apos;probability&apos;</span>) <span class="hljs-comment"># &#x8BBE;&#x7F6E;&#x6A2A;&#x8F74;&#x6807;&#x7B7E;</span>
ax.set_title(<span class="hljs-string">u&apos;Top-5&apos;</span>) <span class="hljs-comment"># &#x6DFB;&#x52A0;&#x6807;&#x9898;</span>
<span class="hljs-keyword">for</span> a,b <span class="hljs-keyword">in</span> zip(range(len(values)), values):
  <span class="hljs-comment"># &#x5728;&#x6BCF;&#x4E2A;&#x67F1;&#x5B50;&#x7684;&#x9876;&#x7AEF;&#x6DFB;&#x52A0;&#x5BF9;&#x5E94;&#x7684;&#x9884;&#x6D4B;&#x6982;&#x7387;&#x503C;&#xFF0C;a&#xFF0C;b &#x8868;&#x793A;&#x5750;&#x6807;&#xFF0C;b+0.0005 &#x8868;&#x793A;&#x8981;&#x628A;&#x6587;&#x672C;&#x4FE1;&#x606F;&#x653E;&#x7F6E;&#x5728;&#x9AD8;&#x4E8E;&#x6BCF;&#x4E2A;&#x67F1;&#x5B50;&#x9876;&#x7AEF; 0.0005 &#x7684;&#x4F4D;&#x7F6E;&#xFF0C;</span>
  <span class="hljs-comment"># center &#x662F;&#x8868;&#x793A;&#x6587;&#x672C;&#x4F4D;&#x4E8E;&#x67F1;&#x5B50;&#x9876;&#x7AEF;&#x6C34;&#x5E73;&#x65B9;&#x5411;&#x4E0A;&#x7684;&#x7684;&#x4E2D;&#x95F4;&#x4F4D;&#x7F6E;&#xFF0C;bottom &#x662F;&#x5C06;&#x6587;&#x672C;&#x6C34;&#x5E73;&#x653E;&#x7F6E;&#x5728;&#x67F1;&#x5B50;&#x9876;&#x7AEF;&#x5782;&#x76F4;&#x65B9;&#x5411;&#x4E0A;&#x7684;&#x5E95;&#x7AEF; &#x4F4D;&#x7F6E;&#xFF0C;fontsize &#x662F;&#x5B57;&#x53F7;</span>
  ax.text(a, b+<span class="hljs-number">0.0005</span>, utils.percent(b), ha=<span class="hljs-string">&apos;center&apos;</span>, va = <span class="hljs-string">&apos;bottom&apos;</span>, fontsize=<span class="hljs-number">7</span>)
plt.savefig(<span class="hljs-string">&apos;./result.jpg&apos;</span>)<span class="hljs-comment"># &#x4FDD;&#x5B58;&#x56FE;&#x7247;</span>
plt.show() <span class="hljs-comment"># &#x5F39;&#x7A97;&#x5C55;&#x793A;&#x56FE;&#x50CF;(linux &#x670D;&#x52A1;&#x5668;&#x4E0A;&#x5C06;&#x8BE5;&#x53E5;&#x6CE8;&#x91CA;&#x6389;</span>
</code></pre>
<h2 id="&#x8BFE;&#x7A0B;&#x4E2D;&#x63D0;&#x5230;&#x7684;&#x7EC3;&#x4E60;&#x5185;&#x5BB9;">&#x8BFE;&#x7A0B;&#x4E2D;&#x63D0;&#x5230;&#x7684;&#x7EC3;&#x4E60;&#x5185;&#x5BB9;</h2>
<p>&#x6253;&#x5370;&#x51FA;<code>img_ready</code>&#x7684;&#x7EF4;&#x5EA6;:</p>
<p><code>app.py</code>&#x7B2C; 11 &#x884C;&#x52A0;&#x5165;<code>print(&quot;img_ready shape&quot;, tf.Session().run(tf.shape(img_ready)))</code></p>
<pre><code class="lang-python"><span class="hljs-comment">#coding:utf-8</span>
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-keyword">import</span> vgg16
<span class="hljs-keyword">import</span> utils
<span class="hljs-keyword">from</span> Nclasses <span class="hljs-keyword">import</span> labels

img_path = raw_input(<span class="hljs-string">&apos;Input the path and image name:&apos;</span>)
img_ready = utils.load_image(img_path) <span class="hljs-comment">#[1, 224, 224, 3]</span>
print(<span class="hljs-string">&quot;img_ready shape&quot;</span>, tf.Session().run(tf.shape(img_ready)))
</code></pre>
<p>&#x8F93;&#x51FA;:</p>
<p><code>img_ready shape [1 224 224 3]</code>s</p>
<p>&#x4E0A;&#x4E0B;&#x6587;&#x7BA1;&#x7406;&#x5668;</p>
<pre><code class="lang-python"><span class="hljs-keyword">with</span> tf.variable_scope(<span class="hljs-string">&quot;foo&quot;</span>):
  <span class="hljs-keyword">with</span> tf.variable_scope(<span class="hljs-string">&quot;bar&quot;</span>):
    v = tf.get_variable(<span class="hljs-string">&quot;v&quot;</span>, [<span class="hljs-number">1</span>])
    <span class="hljs-keyword">assert</span> v.name == <span class="hljs-string">&quot;foo/bar/v:0&quot;</span>
</code></pre>
<p>&#x51FA;&#x73B0;&#x547D;&#x540D;&#x5C42;&#x6B21;&#x7ED3;&#x6784; foo/bar/v</p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; scottdu 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2018-08-18 15:53:44
</span></footer> <link rel="stylesheet" type="text/css" href="https://storage.googleapis.com/app.klipse.tech/css/codemirror.css"> <script>     window.klipse_settings = {         selector: ".language-klipse, .lang-eval-clojure",         selector_eval_js: ".lang-eval-js",         selector_eval_python_client: ".lang-eval-python",         selector_eval_php: ".lang-eval-php",         selector_eval_scheme: ".lang-eval-scheme",         selector_eval_ruby: ".lang-eval-ruby",         selector_reagent: ".lang-reagent",        selector_google_charts: ".lang-google-chart",        selector_es2017: ".lang-eval-es2017",        selector_jsx: ".lang-eval-jsx",        selector_transpile_jsx: ".lang-transpile-jsx",        selector_render_jsx: ".lang-render-jsx",        selector_react: ".lang-react",        selector_eval_markdown: ".lang-render-markdown",        selector_eval_lambdaway: ".lang-render-lambdaway",        selector_eval_cpp: ".lang-eval-cpp",        selector_eval_html: ".lang-render-html",        selector_sql: ".lang-eval-sql",        selector_brainfuck: "lang-eval-brainfuck",        selector_js: ".lang-transpile-cljs"    }; </script> <script src="https://storage.googleapis.com/app.klipse.tech/plugin/js/klipse_plugin.js"></script>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../chapter5/section6.1.html" class="navigation navigation-prev " aria-label="Previous page: 第一节 复现已有的卷积神经网络">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../chapter7/" class="navigation navigation-next " aria-label="Next page: 第七章 Tensorflow应用">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第二节 用vgg16实现图片识别","level":"1.7.2","depth":2,"next":{"title":"第七章 Tensorflow应用","level":"1.8","depth":1,"path":"chapter7/README.md","ref":"chapter7/README.md","articles":[]},"previous":{"title":"第一节 复现已有的卷积神经网络","level":"1.7.1","depth":2,"path":"chapter5/section6.1.md","ref":"chapter5/section6.1.md","articles":[]},"dir":"ltr"},"config":{"plugins":["katex","expandable-chapters-small","tbfed-pagefooter","alerts","copy-code-button","puml","graph","chart","klipse","donate","simple-page-toc","splitter"],"root":".","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy scottdu 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"puml":{},"simple-page-toc":{"maxDepth":3,"skipFirstH1":true},"splitter":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"graph":{},"donate":{"alipay":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-alipay1.jpeg","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-wechatpay.png","wechatText":"微信打赏"},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"alerts":{},"expandable-chapters-small":{},"copy-code-button":{},"klipse":{"myConfigKey":"it's the default value"},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"chart":{"type":"c3"}},"theme":"default","author":"scottdu","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Tensorflow学习笔记","language":"zh-hans","gitbook":"3.2.3","description":"记录Tensorflow的学习内容"},"file":{"path":"chapter6/section6.2.md","mtime":"2018-08-18T07:53:44.139Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-25T05:55:22.995Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

